﻿class Logo extends MovieClip
{
	//physics model
	private var MY_K:Number = 0.1;
	
	private var MY_X:Number;
	private var MY_Y:Number;
	
	private var my_p_root:MovieClip; //pointer to root
	
	private var points:Array;
	private var gfx:MovieClip; //gfx context;
	private var update_interval:Number; //interval object to implement physics model
	
	private var outline:Number;
	private var fill:Number;
	private var alpha:Number;
	
	function Logo(p_root:MovieClip, new_x:Number, new_y:Number, col1:Number, col2:Number, col3:Number)
	{
		//trace ("new logo");
		//initialise
		MY_X = new_x;
		MY_Y = new_y;
		
		outline = col1;
		fill = col2;
		alpha = col3;
		
		my_p_root = p_root;
		
		//establish a graphics context
		gfx = my_p_root.createEmptyMovieClip("gfx",my_p_root.getNextHighestDepth(),{_x:MY_X, _y:MY_Y});
		
		//initialise array with objects
		points = new Array(83);
		for (var i:Number = 0; i<points.length; i++)
		{
			points[i] = new Object();
		}
		//populate objects with x / y values
		
		//M
		//M
		points[0].x_pos = 200;
		points[0].y_pos = 30;
		points[1].x_pos = 200;
		points[1].y_pos = 80;
		points[2].x_pos = 210;
		points[2].y_pos = 80;
		points[3].x_pos = 210;
		points[3].y_pos = 50;
		points[4].x_pos = 220;
		points[4].y_pos = 50;
		points[5].x_pos = 220;
		points[5].y_pos = 80;
		points[6].x_pos = 230;
		points[6].y_pos = 80;
		points[7].x_pos = 230;
		points[7].y_pos = 30;
		points[8].x_pos = 220;
		points[8].y_pos = 30;
		points[9].x_pos = 220;
		points[9].y_pos = 40;
		points[10].x_pos = 210;
		points[10].y_pos = 40;
		points[11].x_pos = 210;
		points[11].y_pos = 30;
		//U
		points[12].x_pos = 240;
		points[12].y_pos = 30;
		points[13].x_pos = 240;
		points[13].y_pos = 70;
		points[14].x_pos = 250;
		points[14].y_pos = 70;
		points[15].x_pos = 250;
		points[15].y_pos = 80;
		points[16].x_pos = 260;
		points[16].y_pos = 80;
		points[17].x_pos = 260;
		points[17].y_pos = 70;
		points[18].x_pos = 270;
		points[18].y_pos = 70;
		points[19].x_pos = 270;
		points[19].y_pos = 30;
		points[20].x_pos = 260;
		points[20].y_pos = 30;
		points[21].x_pos = 260
		points[21].y_pos = 70;
		points[22].x_pos = 250;
		points[22].y_pos = 70;
		points[23].x_pos = 250;
		points[23].y_pos = 30;
		//S part one
		points[24].x_pos = 280;
		points[24].y_pos = 40;
		points[25].x_pos = 280;
		points[25].y_pos = 50;
		points[26].x_pos = 290;
		points[26].y_pos = 50;
		points[27].x_pos = 290;
		points[27].y_pos = 40;
		points[28].x_pos = 290;
		points[28].y_pos = 30;
		points[29].x_pos = 300;
		points[29].y_pos = 30;
		points[30].x_pos = 300;
		points[30].y_pos = 40;
		points[31].x_pos = 290
		points[31].y_pos = 40;
		points[32].x_pos = 280;
		points[32].y_pos = 40;
		//S part two
		points[33].x_pos = 280;
		points[33].y_pos = 70;
		points[34].x_pos = 280;
		points[34].y_pos = 80;
		points[35].x_pos = 290;
		points[35].y_pos = 80;
		points[36].x_pos = 290;
		points[36].y_pos = 70;
		points[37].x_pos = 300;		
		points[37].y_pos = 70;
		points[38].x_pos = 300;
		points[38].y_pos = 60;
		points[39].x_pos = 290
		points[39].y_pos = 60;
		points[40].x_pos = 290;
		points[40].y_pos = 70;
		//I
		points[41].x_pos = 310;
		points[41].y_pos = 30;
		points[42].x_pos = 310;
		points[42].y_pos = 80;
		points[43].x_pos = 320;
		points[43].y_pos = 80;
		points[44].x_pos = 320;
		points[44].y_pos = 30;
		//P
		points[45].x_pos = 330;
		points[45].y_pos = 30;
		points[46].x_pos = 330;
		points[46].y_pos = 80;
		points[47].x_pos = 340;
		points[47].y_pos = 80;
		points[48].x_pos = 340;
		points[48].y_pos = 60;
		points[49].x_pos = 350;
		points[49].y_pos = 60;
		points[50].x_pos = 350;
		points[50].y_pos = 30;
		//E
		points[51].x_pos = 360;
		points[51].y_pos = 30;
		points[52].x_pos = 360;
		points[52].y_pos = 80;
		points[53].x_pos = 380;
		points[53].y_pos = 80;
		points[54].x_pos = 380;
		points[54].y_pos = 70;
		points[55].x_pos = 370;
		points[55].y_pos = 70;
		points[56].x_pos = 370;
		points[56].y_pos = 60;
		points[57].x_pos = 380;
		points[57].y_pos = 60;
		points[58].x_pos = 380;
		points[58].y_pos = 50;
		points[59].x_pos = 370;
		points[59].y_pos = 50;
		points[60].x_pos = 370;
		points[60].y_pos = 40;
		points[61].x_pos = 380;
		points[61].y_pos = 40;
		points[62].x_pos = 380;
		points[62].y_pos = 30;
		//D
		points[63].x_pos = 390;
		points[63].y_pos = 30;
		points[64].x_pos = 390;
		points[64].y_pos = 80;
		points[65].x_pos = 400;
		points[65].y_pos = 80;
		points[66].x_pos = 400;
		points[66].y_pos = 70;
		points[67].x_pos = 410;
		points[67].y_pos = 70;
		points[68].x_pos = 410;
		points[68].y_pos = 40;
		points[69].x_pos = 400;
		points[69].y_pos = 40;
		points[70].x_pos = 400;
		points[70].y_pos = 30;
		//E
		points[71].x_pos = 420;
		points[71].y_pos = 30;
		points[72].x_pos = 420;
		points[72].y_pos = 80;
		points[73].x_pos = 440;
		points[73].y_pos = 80;
		points[74].x_pos = 440;
		points[74].y_pos = 70;
		points[75].x_pos = 430;
		points[75].y_pos = 70;
		points[76].x_pos = 430;
		points[76].y_pos = 60;
		points[77].x_pos = 440;
		points[77].y_pos = 60;
		points[78].x_pos = 440;
		points[78].y_pos = 50;
		points[79].x_pos = 430;
		points[79].y_pos = 50;
		points[80].x_pos = 430;
		points[80].y_pos = 40;
		points[81].x_pos = 440;
		points[81].y_pos = 40;
		points[82].x_pos = 440;
		points[82].y_pos = 30;
		
		//populate objects with angles derived from x / y positions
		for (var i:Number = 0; i<points.length; i++)
		{	
			//initialise direction of travel and initial position
			points[i].angle = calc_ang(points[i].x_pos, points[i].y_pos);
			points[i].init_dist = get_distance(points[i].x_pos, points[i].y_pos);
		}
		//interval to update physical model
		update_interval = setInterval(this,"update", 10);
    }
	public function set extension(xi:Number)
	{
		//trace("Extension in: " + xi);
		for (var i:Number = 0; i<points.length; i++)
		{
			points[i].x_pos += xi * Math.cos(points[i].angle);
			points[i].y_pos += xi * Math.sin(points[i].angle);
		}
	}
	public function clear_gfx (Void):Void
	{
		gfx.removeMovieClip();
	}
	private function update(Void):Void
	{
		this.move_units();
		this.draw_points();
	}
	private function calc_ang(point_x:Number, point_y:Number):Number
	{
		return(Math.atan2(point_y - MY_Y, point_x - MY_X) );//angle to centre
	}
	private function get_distance(x_loc:Number, y_loc:Number):Number
	{
		return ( Math.sqrt(Math.pow((MY_X - x_loc),2) + Math.pow((MY_Y - y_loc),2) ) );
	}
	private function move_units(Void):Void
	{	
		for (var i:Number = 0; i<points.length; i++)
		{
			//spring force
			var force:Number = -MY_K * (get_distance(points[i].x_pos, points[i].y_pos) - points[i].init_dist);
			
			points[i].x_pos += force * Math.cos(points[i].angle);
			points[i].y_pos += force * Math.sin(points[i].angle);
		}
	}
	private function draw_points(Void):Void
	{
		gfx.clear();
		gfx.lineStyle(1,outline); 
		//M
		gfx.beginFill(fill,alpha); 
		gfx.moveTo(points[0].x_pos, points[0].y_pos);
		for (var i:Number = 0; i<=11; i++)
		{
			gfx.lineTo(points[i].x_pos, points[i].y_pos);
		}
		gfx.endFill();
		
		//U
		gfx.beginFill(fill, alpha); 
		gfx.moveTo(points[12].x_pos, points[12].y_pos);
		for (var i:Number = 12; i<=23; i++)
		{
			gfx.lineTo(points[i].x_pos, points[i].y_pos);
		}
		gfx.endFill();
		
		//S part one
		gfx.beginFill(fill, alpha); 
		gfx.moveTo(points[24].x_pos, points[24].y_pos);
		for (var i:Number = 24; i<=32; i++)
		{
			gfx.lineTo(points[i].x_pos, points[i].y_pos);
		}
		gfx.endFill();
		//S part two
		gfx.beginFill(fill, alpha); 
		gfx.moveTo(points[33].x_pos, points[33].y_pos);
		for (var i:Number = 33; i<=40; i++)
		{
			gfx.lineTo(points[i].x_pos, points[i].y_pos);
		}
		gfx.endFill();
		//I
		gfx.beginFill(fill, alpha); 
		gfx.moveTo(points[41].x_pos, points[41].y_pos);
		for (var i:Number = 41; i<=44; i++)
		{
			gfx.lineTo(points[i].x_pos, points[i].y_pos);
		}
		gfx.endFill();
		//P
		gfx.beginFill(fill, alpha); 
		gfx.moveTo(points[45].x_pos, points[45].y_pos);
		for (var i:Number = 45; i<=50; i++)
		{
			gfx.lineTo(points[i].x_pos, points[i].y_pos);
		}
		gfx.endFill();
		//E
		gfx.beginFill(fill, alpha); 
		gfx.moveTo(points[51].x_pos, points[51].y_pos);
		for (var i:Number = 51; i<=62; i++)
		{
			gfx.lineTo(points[i].x_pos, points[i].y_pos);
		}
		gfx.endFill();
		//D
		gfx.beginFill(fill, alpha); 
		gfx.moveTo(points[63].x_pos, points[63].y_pos);
		for (var i:Number = 63; i<=70; i++)
		{
			gfx.lineTo(points[i].x_pos, points[i].y_pos);
		}
		gfx.endFill();
		//E
		gfx.beginFill(fill, alpha); 
		gfx.moveTo(points[71].x_pos, points[71].y_pos);
		for (var i:Number = 71; i<=82; i++)
		{
			gfx.lineTo(points[i].x_pos, points[i].y_pos);
		}
		gfx.endFill();
	}
}